/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.Receita_CRUD;
import Model.Comentario;
import Model.Nota_receita;
import Model.Receita;
import Model.Usuario;
import Util.HibernateUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Session;

/**
 *
 * @author aluno
 */
@WebServlet(name = "ServletReceita", urlPatterns = {"/ServletReceita"})
public class ServletReceita extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        Usuario user = (Usuario)session.getAttribute("user");
        String opt = (String) request.getParameter("opt");
        Receita receita;
        String url = "";
        Integer id;
        ArrayList myrecs;
        switch (opt) {
            case "insert":
                session.removeAttribute("receita");
                Receita_CRUD.insertReceita(request);
                myrecs = Receita_CRUD.getmyrecs(request, "yes",user);
                session.setAttribute("myrecs", myrecs);
                url = "main.jsp";
                break;
            case "cad":
                String aux = (String)request.getParameter("edit");
                id = 0;
                if(aux != null){
                    id = Integer.valueOf(aux);
                }
                receita = (Receita) Receita_CRUD.detalharReceita(request, id);
                Receita rec = new Receita();
                rec = receita;
                session.setAttribute("receita", rec);
                url = "cadastro_receita.jsp";
                break;
            case "list":
                Receita_CRUD.listReceitaDisp(request,user);
                url = "minhas_receitas.jsp";
                break;
            case "detalhe":
                id = Integer.valueOf(request.getParameter("id"));
                receita = (Receita) Receita_CRUD.detalharReceita(request, id);
                session.setAttribute("receita", receita);
                ArrayList <Comentario> comentarios = Receita_CRUD.comentariosReceita(id);
                session.setAttribute("comentarios", comentarios);
                url = "detalhe_receita.jsp";
               
                break;
            case "deletar":
                Receita_CRUD.deletarReceita(request);
                session.setAttribute("recs", Receita_CRUD.listReceitas());           
                url = "gerenciamento.jsp";
                break;
        }
        RequestDispatcher dispatcher = request.getRequestDispatcher(url);
        dispatcher.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
